package com.mashibing.juc.c_008.interview;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @ClassName T07_LockSupport_WithoutSleep
 * @Description LockSupport WithoutSleep
 * @Author xiongwei.wu
 * @Date 2021/7/13 10:07
 **/
public class T07_LockSupport_WithoutSleep {
    volatile List<Object> lists = new LinkedList();

    private void add(Object object) {
        lists.add(object);
    }

    private int size() {
        return lists.size();
    }

    static Thread t1, t2;

    public static void main(String[] args) {
        T07_LockSupport_WithoutSleep t = new T07_LockSupport_WithoutSleep();

        t1 = new Thread(() -> {
            System.out.println("T1启动");
            for (int i = 0; i < 10; i++) {
                t.add(new Object());
                System.out.println("add: " + i);
                if (i == 5) {
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
            }
            System.out.println("T1结束");
        }, "T1");

        t2 = new Thread(() -> {
            System.out.println("T2启动");
            if (t.size() != 5) {
                LockSupport.park();
            }
            System.out.println("T2结束");
            LockSupport.unpark(t1);
        }, "T2");

        t2.start();
        t1.start();
    }
}
